package com.cmo.common.controller.paiban;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cmo.common.model.RltDoctor2queueType;
import com.cmo.common.model.RltPager2queueType;
import com.cmo.common.model.Triage;
import com.cmo.common.utils.Shared;
import com.cmo.common.utils.ToolString;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

public class DoctorOrPagerPaibanController extends Controller{
	Shared shared=new Shared();
	//分诊台
	Triage triage=new Triage();
	//医生排班
	RltDoctor2queueType rdq=new RltDoctor2queueType();
	//叫号器排班
	RltPager2queueType rpq=new RltPager2queueType();
	static final String CODE="RltDoctor2queueType";
	/**
	 * 分诊台分诊类型按医生的排班
	 * triage_type1按医生 2按叫号器
	 */
	public void DoctorOrPagerPaiban(){
		Map<String, Object> map=new HashMap<String, Object>();
		//按医生
		String ip=shared.getIpAddr(getRequest());
		System.out.println("ip:"+ip);
			Record rip= triage.findByIP(ip);
			if(null==rip){
				map.put("return_code", "fail");
				map.put("return_msg", "当前电脑没有分诊台配置");
				renderJson(map);
				return;
			}
			else{
				String tip=rip.getStr("ip");
				Record rt= triage.findByIP(tip);
				if("1".equals(rt.get("triage_type").toString())){
					List<Record> list=rdq.findDoctorPaiban(tip);
					if(null==list){
						map.put("return_code", "fail");
						map.put("return_msg", "查询按医生类型排班失败或没有排班信息");
						renderJson(map);
						return;
					}else{
						map.put("return_code", "success");
						map.put("return_msg", "查询按医生类型排班成功");
						map.put("list", list);
						map.put("count", list.size());
						renderJson(map);
					}
				}else if("2".equals(rt.get("triage_type").toString())){//按叫号器
					List<Record> list=rpq.pager_paiban(tip);
					if(null==list){
						map.put("return_code", "fail");
						map.put("return_msg", "查询按叫号器类型排班失败或没有排班信息");
						renderJson(map);
						return;
					}else{
						map.put("return_code", "success");
						map.put("return_msg", "查询按叫号器排班信息成功");
						map.put("list", list);
						map.put("count", list.size());
						renderJson(map);
					}
				}else{
					map.put("return_code", "fail");
					map.put("return_msg", "系统错误，请联系管理员");
					renderJson(map);
					return;
				}
			}
		
		renderJson(map);
	}
	public void list_paiban(){
		Map<String, Object> map=new HashMap<String, Object>();
		String ip=shared.getIpAddr(getRequest());
		if(ToolString.isBlank(ip)){
			map.put("return_code", "fail");
			map.put("return_msg", "获取分诊台ip失败");
			renderJson(map);
			return;
		}
		List<Record> list= triage.queryByQueueType(ip);
		if(null==list||list.size()<1){
			map.put("return_code", "fail");
			map.put("return_msg", "当前分诊台下没有队列列表");
			renderJson(map);
			return;
		}else{
			map.put("return_code", "success");
			map.put("return_msg", "查询成功");
			map.put("list",list);
			renderJson(map);
		}
		renderJson(map);
	}
	public void update_pb(){
		Map<String, Object> map=new HashMap<String, Object>();
		String ids=getPara("ids");
		if(ToolString.isBlank(ids)){
			map.put("return_code", "fail");
			map.put("return_msg", "ID不能为空");
			renderJson(map);
			return;
		}
		ids = ids.substring(0,ids.length()-1);
		String removestr="d_";
		ids=ids.replaceAll(removestr, "");
		String onduty=getPara("onduty");
		if(ToolString.isBlank(onduty)){
			map.put("return_code", "fail");
			map.put("return_msg", "排班不能为空");
			renderJson(map);
			return;
		}
		int i= rdq.update_rdq(ids, onduty);
		if(i>0){
			map.put("return_code", "success");
			map.put("return_msg", "成功");
			renderJson(map);
		}else{
			map.put("return_code", "fail");
			map.put("return_msg", "失败");
			renderJson(map);
			return;
		}
		renderJson(map);
	}
	
	public void DoctorOrPagerPaibanAdd(){
		Map<String, Object> map=new HashMap<String, Object>();
		String doctor_id=getPara("doctor_id");
		if(ToolString.isBlank(doctor_id)){
			map.put("return_code", "fail");
			map.put("return_msg", "医生不能为空");
			renderJson(map);
			return;
		}
		String queue_type_id=getPara("queue_type_id");
		if(ToolString.isBlank(queue_type_id)){
			map.put("return_code", "fail");
			map.put("return_msg", "队列不能为空");
			renderJson(map);
			return;
		}
		String onduty=getPara("onduty");
		if(ToolString.isBlank(onduty)){
			map.put("return_code", "fail");
			map.put("return_msg", "排班不能为空");
			renderJson(map);
			return;
		}
		Record record= rdq.finddoctorandqueuetypeid(doctor_id, queue_type_id);
		if(null!=record){
			map.put("return_code", "fail");
			map.put("return_msg", "当前医生排班已存在");
			renderJson(map);
			return;
		}
		rdq.setDoctorId(Integer.parseInt(doctor_id));
		rdq.setQueueTypeId(Integer.parseInt(queue_type_id));
		rdq.setOnduty(onduty);
		boolean bool= rdq.save();
		if(bool){
			map.put("return_code", "success");
			map.put("return_msg", "成功");
			renderJson(map);
		}else{
			map.put("return_code", "fail");
			map.put("return_msg", "失败");
			renderJson(map);
			return;
		}
		renderJson(map);
	}
	public void delete_pb(){
		Map<String, Object> map=new HashMap<String, Object>();
		String ids=getPara("ids");
		if(ToolString.isBlank(ids)){
			map.put("return_code", "fail");
			map.put("return_msg", "不能为空");
			renderJson(map);
			return;
		}
		ids = ids.substring(0,ids.length()-1);
		String removestr="d_";
		ids=ids.replace(removestr, "");
		String[] strArray=null;
		strArray=ids.split(",");
		System.out.println(strArray);
		boolean bool=false;
		for (int i=0;i<strArray.length;i++) {
			bool=rdq.deleteById(strArray[i]);
		}
		if(bool){
			map.put("return_code", "success");
			map.put("return_msg", "成功");
			renderJson(map);
		}else{
			map.put("return_code", "fail");
			map.put("return_msg", "失败");
			renderJson(map);
			return;
		}
		renderJson(map);
	}
}
